let city = ''
$.ajax({
  url: "./register/city.json",
  data: { action: "jQueryAjax" },
  type: "GET",
  async: false,
  success: function (data) {
    city = data
  },
  //返回数据的格式
  dataType: "json"
})

new Vue({
  el: '#app',
  data() {
    return {
      // 整个省市县数据
      city,
      // 被选中的市数据
      cityList: [],
      // 被选中的县数据
      areaList: [],
      selectProvince: {},
      selectCity: {},
      selectArea: {},
      address: '',
      isdisabled: true,
      enterprise_info: {//提交的表单
        Enterprise_Info_SerialNumber: '',//主键
        Enterprise_Name:"",//企业名称
        Industry_SubCategory_SerialNumber:"101000000000001",
        Industry_Category_SerialNumber:"100000000000001",
        Enterprise_Address: '',//企业注册地址
        Postal_Code: '',//邮政编号
        Tax_Number: '',//营业执照注册号
        Tax_Registration_Number: '',//税务登记证号
        Organization_Code_Certificate_Number: '',//组织机构代码证号
        Enterprise_Phone: '',//企业电话
        Enterprise_Contacts: '',//企业联系人
        Enterprise_Contacts_Mobile: '',//企业手机号
        E_Mail: '',//电子邮箱
        Children:{
          enterprise_legal_person:[{
            Enterprise_Legal_Person_SerialNumber:"",
            Enterprise_Info_SerialNumoer:"",
            Legal_Person: '',//法人姓名
            ID_Number: '',//法人身份证号
            Mobile: '',//法人手机号
          }],
          enterprise_account:[{
            Enterprise_Account_SerialNumber:"",
            Enterprise_Info_SerialNumoer:"",
            Bank_Name: '',//开户行
            Account_Number: '',//银行账号
          }]
        }
      },
      system_user_info:{
        User_Number:"",
        ID_Name: '',//用户姓名
        ID_SerialNumber: '',//用户身份证号
        User_Number: '',//用户手机号
        Pass_Ward:"",//用户密码
        Enterprise_Info_SerialNumber:""
      },
      Enter_again_Pass_Ward:'',
      rules: {
        Enterprise_Info_SerialNumber: [
          { required: true, message: '请输入企业名称', trigger: 'blur' },
          { min: 2, message: '最少2个字符', trigger: 'blur' }
        ],
        // Enterprise_Address: [
        //   { required: true, message: '请选择企业注册地址', trigger: 'change' },
        // ],
        // qyzcxxdz: [
        //   { required: true, message: '请输入详细地址', trigger: 'blur' },
        // ],
        Postal_Code: [
          { required: true, message: '请输入邮政编号', trigger: 'blur' },
          { min: 6, message: '最少6个字符', trigger: 'blur' }
        ],
        Tax_Number: [
          { required: true, message: '请输入营业执照注册号', trigger: 'blur' },
          { min: 15, message: '营业执照注册号为15位数字', trigger: 'blur' }
        ],
        Tax_Registration_Number: [
          { required: true, message: '请输入税务登记证号', trigger: 'blur' },
          { min: 18, message: '税务登记证号为18位数字', trigger: 'blur' }
        ],
        Organization_Code_Certificate_Number: [
          { required: true, message: '请输入组织机构代码证号', trigger: 'blur' },
          { min: 8, message: '组织机构代码证号为8位字符', trigger: 'blur' }
        ],
        Enterprise_Phone: [
          { required: true, message: '请输入企业电话', trigger: 'blur' },
        ],
        Enterprise_Contacts: [
          { required: true, message: '请输入联系人姓名', trigger: 'blur' },
        ],
        Enterprise_Contacts_Mobile: [
          { required: true, message: '请输入联系人手机号', trigger: 'blur' },
          { min: 11, message: '手机号为11位数字', trigger: 'blur' }
        ],
        E_Mail: [
          { required: true, message: '请输入电子邮箱', trigger: 'blur' },
          { min: 4, message: '输入字符过少', trigger: 'blur' }
        ],
        Account_Name: [
          { required: true, message: '请输入账户名称', trigger: 'blur' },
          { min: 2, message: '输入字符过少', trigger: 'blur' }
        ],
        Bank_Name: [
          { required: true, message: '请选择开户行', trigger: 'change' },
        ],
        Account_Number: [
          { required: true, message: '请输入银行账号', trigger: 'blur' },
          { min: 19, message: '银行账号为19位数字', trigger: 'blur' }
        ],
        Legal_Person: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
        ],
        ID_Number: [
          { required: true, message: '请输入身份证号', trigger: 'blur' },
          { min: 18, message: '身份证号为18位数字', trigger: 'blur' }
        ],
        Mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 11, message: '手机号为11位数字', trigger: 'blur' }
        ],
        ID_Name: [
          { required: true, message: '请输入授权姓名', trigger: 'blur' },
        ],
        ID_SerialNumber: [
          { required: true, message: '请输入授权身份证号', trigger: 'blur' },
          { min: 18, message: '身份证号为18位数字', trigger: 'blur' }
        ],
        User_Number: [
          { required: true, message: '请输入授权手机号', trigger: 'blur' },
          { min: 11, message: '手机号为11位数字', trigger: 'blur' }
        ],
        Pass_Ward: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6,max: 12, message: '密码为6-12位字符', trigger: 'blur' }
        ],
      },
      loading: false,
      message:'',
      message2:'',
      color:'',
      color2:''
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //表单验证之企业名称
          var reg_Enterprise_Info_SerialNumber = /^[\u4e00-\u9fa5]{0,}$/
          //表单验证之邮政编码
          var reg_Postal_Code = /^[1-9]\d{5}$/
          //表单验证之营业执照注册号
          var reg_Tax_Number = /^[0-9a-zA-Z]{15}$|^[0-9a-zA-Z]{18}$/
          //表单验证之税务登记证号
          var reg_Tax_Registration_Number = /^[A-Z0-9]{15}$|^[A-Z0-9]{18}$|^[A-Z0-9]{20}$/
          //表单验证之组织机构代码证号
          var reg_zzjgdmzh = /^[A-Za-z0-9]{8}-[A-Za-z0-9]$/
          //表单验证之企业电话
          var reg_Enterprise_phone = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/
          //表单验证之联系人手机
          var reg_Enterprise_Contacts_Mobile = /^1[3-9]\d{9}$/
          //表单验证之电子邮箱
          var reg_E_Mail = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/
          //表单验证之银行账号
          var reg_yhzh = /^([1-9]{1})(\d{14}|\d{18})$/
          //表单验证之身份证号
          var reg_ID_Number = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/
          // if (!reg_Enterprise_Info_SerialNumber.test(this.enterprise_info.Enterprise_Info_SerialNumber)) {
          //   this.$message({
          //     message: '企业名称应为汉字,且不能带有数字及字符',
          //     type: 'warning'
          //   });
          //   return
          // }
          // if (!reg_Postal_Code.test(this.enterprise_info.Postal_Code)) {
          //   this.$message({
          //     message: '邮政编码应为6位',
          //     type: 'warning'
          //   });
          //   return
          // }
          // if (!reg_Tax_Number.test(this.enterprise_info.Tax_Number)) {
          //   this.$message({
          //     message: '营业执照注册号有误',
          //     type: 'warning'
          //   });
          //   return
          // }
          // if (!reg_Tax_Registration_Number.test(this.enterprise_info.Tax_Registration_Number)) {
          //   this.$message({
          //     message: '税务登记证号有误',
          //     type: 'warning'
          //   });
          //   return
          // }
          // if (!reg_zzjgdmzh.test(this.enterprise_info.Organization_Code_Certificate_Number)) {
          //   this.$message({
          //     message: '组织机构代码证号有误',
          //     type: 'warning'
          //   });
          //   return
          // }
          // if (!reg_Enterprise_phone.test(this.enterprise_info.Enterprise_phone)) {
          //   this.$message({
          //     message: '企业电话有误',
          //     type: 'warning'
          //   });
          //   return
          // }
          // if (!reg_Enterprise_Contacts_Mobile.test(this.enterprise_info.Enterprise_Contacts_Mobile)) {
          //   this.$message({
          //     message: '联系人手机有误',
          //     type: 'warning'
          //   });
          //   return
          // }
          // if (!reg_E_Mail.test(this.enterprise_info.E_Mail)) {
          //   this.$message({
          //     message: '电子邮箱有误',
          //     type: 'warning'
          //   });
          //   return
          // }
          // if (!reg_yhzh.test(this.enterprise_info.Account_Number)) {
          //   this.$message({
          //     message: '银行账号有误',
          //     type: 'warning'
          //   });
          //   return
          // }
          // if (!reg_ID_Number.test(this.enterprise_info.ID_Number)) {
          //   this.$message({
          //     message: '身份证号有误',
          //     type: 'warning'
          //   });
          //   return
          // }
          var _this = this
          this.loading = true
          console.log(this.enterprise_info)
          console.log(this.system_user_info)
          if (this.loading) {
            $.ajax({
              url: DomainName + '/sv/Register_Enterprise',
              type: 'POST',
              data: {
                  system_user_info: JSON.stringify({ system_user_info: [_this.system_user_info] }),
                  enterprise_info: JSON.stringify({ enterprise_info: [_this.enterprise_info] })
              },
              success: function (result) {
                  console.log(result);
                  if (result.Status == "success") {
                      _this.loading = true
                      _this.$message({
                          message: '恭喜你，注册成功',
                          type: 'success'
                      });
                      window.location.href = 'login.html';
                  } else {
                      _this.$message.error(result.Cause);
                  }
              },
              error: function (params) {
                  console.log(params);
              },
              complete: function () { _this.loading = false }
          })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    checkname(e){
      var _this = this
      $.ajax({
        url: DomainName + '/sv/CheckRegisterAction',
        type: 'POST',
        data: {
          Enterprise_Name:e,
        },
        success: function (result) {
            console.log(result);
            if(result.Status=='fail'){
              _this.message2 = '该企业已被注册'
              _this.color2 = 'red'
            }else{
              _this.message2 = ''
            }
        },
        error: function (params) {
            console.log(params);
        },
        complete: function () { _this.loading = false }
    })
    },
    checkphone(e){
      var _this = this
      $.ajax({
        url: DomainName + '/sv/CheckRegisterAction',
        type: 'POST',
        data: {
          User_Number:e,
        },
        success: function (result) {
            console.log(result);
            if(result.Status=='fail'){
              _this.message = '该管理员手机号已被注册'
              _this.color = 'red'
            }else{
              _this.message = ''
            }
        },
        error: function (params) {
            console.log(params);
        },
        complete: function () { _this.loading = false }
    })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 省份 市 县联动
    selectProvinceFun(event) {
      console.log(event)
      if (event) {
        this.cityList = event.children
      } else {
        this.cityList = []
      }
      this.areaList = []
    },
    selectCityFun(event) {
      console.log(event)
      if (event) {
        this.areaList = event.children
      } else {
        this.areaList = []
      }
    },
    selectAreaFun(event) {
      if (event) {
        console.log(event)
        this.isdisabled = false
      } else {
        this.isdisabled = true
      }
    },
    selectAddress(event) {
      if (event && event != '' && this.selectArea.label != undefined) {
        this.enterprise_info.Supplier_Address = this.selectProvince.label + '/'
          + this.selectCity.label + '/'
          + this.selectArea.label + '/'
      }
    }
  }
})